﻿#region USING STATEMENTS
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Fusion.States;
#endregion

#region CODE BODY
namespace Fusion.Management
{
    public enum FadeType { ThroughBlack, ThroughWhite, ThroughImage };
    public static class FStates
    {
        //////////////////////////////////////////////////
        // Data Fields
        //////////////////////////////////////////////////
        private static Stack<BaseState> States = new Stack<BaseState>();
        private static Rectangle screenRect = new Rectangle(0, 0, 800, 480);
        private static Texture2D fadeScreen;
        private static Boolean PlayFade = false;
        private static Single FadeAlpha = 1.0f;
        private static FadeType fade = FadeType.ThroughBlack;

        //////////////////////////////////////////////////
        // Member Properties
        //////////////////////////////////////////////////
        public static Rectangle ScreenRect
        {
            get { return screenRect; }
            set { screenRect = value; }
        }
        public static Texture2D FadeScreen
        {
            get
            {
                if (fadeScreen == null)
                    fadeScreen = FContent.Load<Texture2D>(@"Textures\Fade");

                return fadeScreen;
            }
        }
        public static FadeType Fade
        {
            get { return fade; }
            set { fade = value; }
        }

        //////////////////////////////////////////////////
        // Methods
        //////////////////////////////////////////////////
        public static void PushState(BaseState ToPush)
        {
            States.Push(ToPush);
            ToPush.Enter();
            PlayFade = true;
        }
        public static void PopState()
        {
            States.Pop().Exit();
            PlayFade = true;
        }
        public static void ChangeState(BaseState NewState, Boolean RequiresLoading)
        {
            foreach (BaseState Base in States)
                Base.Exit();

            States.Clear();

            if (NewState == null)
                return;

            if (RequiresLoading)
            {
                States.Push(NewState);
                LoadState Load = new LoadState();
                Load.NextState = NewState;
                States.Push(Load);
                Load.Enter();
                PlayFade = true;
            }
            else
            {
                States.Push(NewState);
                NewState.Enter();
                PlayFade = true;
            }
        }
        public static Boolean Update(Single Delta)
        {
            if (States.Count == 0)
                return false;

            if (States.Peek().Input(Delta) == false)
                return false;

            States.Peek().Update(Delta);

            // Update our Fade
            if (PlayFade)
                FadeAlpha -= 2f * Delta;

            return true;
        }
        public static void Render(Single Delta)
        {
            FCanvas.ClearCanvas();

            if (States.Count > 0)
                States.Peek().Render(Delta);

            if (PlayFade)
            {
                FCanvas.Begin();
                {
                    if (fade == FadeType.ThroughWhite)
                        FCanvas.FillRectangle(ScreenRect, new Color(FadeAlpha, FadeAlpha, FadeAlpha, FadeAlpha));
                    else if (fade == FadeType.ThroughBlack)
                        FCanvas.FillRectangle(ScreenRect, new Color(0f, 0f, 0f, FadeAlpha));
                    else
                        FCanvas.Draw(FadeScreen, ScreenRect, new Color(FadeAlpha, FadeAlpha, FadeAlpha, FadeAlpha));

                    if (FadeAlpha <= 0f)
                    {
                        FadeAlpha = 1f;
                        PlayFade = false;
                    }
                }
                FCanvas.End();
            }
        }
    }
}
#endregion

//////////////////////////////////////////////////////////////////////////////////////////////
// Copyright 2012 Jeffery Thomas Sventora
//////////////////////////////////////////////////////////////////////////////////////////////